package com.jacky.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * Created by jacky on 2018/2/11.
 */
public class CountDownLatchDemo {

    public static void main(String[] args) {
        Thread mainThread = Thread.currentThread();
        //CountDownLatch latch = new CountDownLatch(4);
        MyCountDownLatch latch = new MyCountDownLatch(4);
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep((int)(Math.random()*100));

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("currentThread--"+Thread.currentThread().getName()+"--mainThread--"+mainThread.getName()
                +"--mainThreadStatus--"+mainThread.getState());
                latch.countDown();
            }
        };
        Thread thread1 = new Thread(runnable);
        Thread thread2 = new Thread(runnable);
        Thread thread3 = new Thread(runnable);
        Thread thread4 = new Thread(runnable);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("mainThread end");
    }
}
